package com.infore.airsupervise.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.ui.activity.PsonContactActivity;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @features:
 * @author: WuTao
 * @date: 2017/5/6 16:13
 */
public class PsonSelectorFragment extends BaseFragment {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private BaseQuickAdapter<UserE,BaseViewHolder> adapter;

    private List<UserE>  userEList= new ArrayList<>();
    private int importPson = 0 ; //主要任务负责人标识

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pson_selector, container, false);
        unbinder = ButterKnife.bind(this, view);
        initRecyclerView();
        return view;
    }
    private void initRecyclerView(){

        recyclerView.setLayoutManager( new GridLayoutManager(getContext(),2));
        adapter = new BaseQuickAdapter<UserE, BaseViewHolder>(R.layout.item_selector,userEList) {
            @Override
            protected void convert(final BaseViewHolder helper, UserE item) {
                if(importPson ==helper.getAdapterPosition()){
                    helper.setTextColor(R.id.tv_title,R.color.red_icon);
                }
                helper.setText(R.id.tv_title,item.getRealName());
                ImageView delete= helper.getView(R.id.iv_delete);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        adapter.remove(helper.getAdapterPosition());
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
    }

    @OnClick(R.id.btn_plus)
    public void onClick() {
        //添加
        Intent it = new Intent(getActivity(), PsonContactActivity.class);
        startActivityForResult(it,400);
    }

    boolean isFirst ;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        isFirst =true;
        if(resultCode == getActivity().RESULT_OK && requestCode == 400){
            UserE userE = (UserE) data.getSerializableExtra(Constants.INTENT_OBJ_KEY);
            for(UserE e : userEList){
                if(TextUtils.equals(e.getAccount(),userE.getAccount())){
                    isFirst = false;
                    return ;
                }
            }
            if(isFirst){
                userEList.add(userE);
                adapter.notifyDataSetChanged();
            }
        }
    }


    //添加当前的用户为默认的人员
    public void addDefaultPson(){
        this.userEList.add(UserE.getUserInfo());
    }

    /**
     * 添加一个人员到集合
     * @param userE
     */
    public void addPson(UserE userE){
        if( null == userEList){
            return ;
        }
        userEList.add(userE);
    }

    /**
     * 添加一个集合
     * @param userEList
     */
    public void addPsonList(List<UserE> userEList){
        if(null != userEList) {
            this.userEList.addAll(userEList);
        }
    }

    /**
     * 获取人员
     * @return
     */
    public  String gethandlePsonId(){
        String handlePsonId ="";
        for(int i = 0 ;i<userEList.size() ; i++){
            if(i>0){
                handlePsonId += userEList.get(i).getUserId() ;
                if(i<userEList.size()-1){
                    handlePsonId+=",";
                }
            }
        }
        return handlePsonId;
    }

    /**
     * 获取人员
     * @return
     */
    public  String getLeaderId(){
        if(userEList.size()>0) {
            return userEList.get(0).getUserId();
        }else {
            return "";
        }
    }

    /**
     * 获取人员ID
     * @return
     */
    public String getAllPson(){
        String psonId ="";
        for(int i = 0 ;i<userEList.size() ; i++){
            psonId += userEList.get(i).getUserId() ;
                if(i<userEList.size()-1){
                    psonId+=",";
                }
        }
        return psonId;
    }

    /**
     * 获取人员名字
     * @return
     */
    public List<UserE> getAllPsonList(){
        return userEList;
    }
}
